package com.jiai.cloud.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.IdGenerate;
import com.jiai.cloud.base.util.ValidateCode;
import com.jiai.cloud.entity.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jiai.cloud.sys.service.ITUserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户基础表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2017-05-23
 */
@RestController
public class TUserController {
    @Autowired
    private ITUserService itUserService;


    @RequestMapping("/loadUser")
    public Object loadUser(){
        return  itUserService.selectId();
    }
    //注册
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Object register(@RequestBody TUser user)throws ServiceException {
        try {
            user.setUserId(IdGenerate.generateUserId());
            itUserService.insert(user);
            return RestResultGenerator.genSuccessResult("注册成功");
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }

    }

    /**
     * 响应验证码页面
     * @return
     */
    @RequestMapping(value="/validateCode" ,method=RequestMethod.GET)
    public String validateCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(120,40,5,100);
        session.setAttribute("code", vCode.getCode());
        vCode.write(response.getOutputStream());
        return null;
    }
    //登录
    @RequestMapping(value = "/templates" ,method=RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object  login(String loginName ,String password,String  code,HttpServletRequest request) throws ServiceException{
        //获取图片
        HttpSession session = request.getSession();
        String sessionCode = (String) session.getAttribute("code");
        if (!code.equalsIgnoreCase(sessionCode)) {  //忽略验证码大小写
            return RestResultGenerator.genSuccessResult("验证码错误");
        }else{
            TUser tUser=itUserService.selectOne(new EntityWrapper<TUser>().eq("loginName",loginName).or("email[0]",loginName));
                if (tUser!=null&&tUser.getPassword().equals(password)){
                    return RestResultGenerator.genSuccessResult("登录成功");
                }else {
                    return  RestResultGenerator.genSuccessResult("用户或密码错误");
                }
        }

    }
    //修改密码
    @RequestMapping(value="/alterogin",method=RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object alterogin(String email, String password) throws ServiceException {
        try {
        TUser user=new TUser();
        user.setPassword(password);
        itUserService.update( user,new EntityWrapper<TUser>().eq("email",email));
        return  RestResultGenerator.genSuccessResult("修改成功");
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

}
